package cn.monkey.chat.server.connect.chat.protocol.http.ws;

import cn.monkey.chat.server.proto.Package;
import cn.monkey.io.transport.netty.codec.SimpleChannelOutboundHandler;
import com.google.gson.Gson;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

public class TextChatMessageOutboundHandler extends SimpleChannelOutboundHandler<cn.monkey.chat.server.proto.Package> {

    private final Gson gson = new Gson();

    @Override
    protected void write0(ChannelHandlerContext channelHandlerContext, Package pkg, ChannelPromise channelPromise) throws Exception {
        channelHandlerContext.write(new TextWebSocketFrame(this.gson.toJson(pkg)), channelPromise);
    }
}
